package kr.co.exight.onApp;

import library.ExPreferenceManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;

public class ActivityShuttle extends Activity implements OnClickListener {

	
	private Button btnToggle;
	private ExPreferenceManager pm;
	private WebView wv;
	
	private final static String SINCHON_URL = "http://yonseihouse.com/bus/image";
	private final static String YIC_URL = "http://yonseihouse.com/bus/image/1";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_shuttle);

		wv = (WebView)findViewById(R.id.shuttle_WebViewBar);

		String mImageUrl = SINCHON_URL;
		
		
		btnToggle = (Button)findViewById(R.id.shuttle_btnToggle);
		btnToggle.setOnClickListener(this);
		
		pm = new ExPreferenceManager(this);
		
		if(pm.getItem("MODE_YIC").equals("true")){
			btnToggle.setText("신촌");
			mImageUrl = YIC_URL;
		}
		
		
		wv.setVisibility(View.VISIBLE);
		wv.setWebViewClient(new WebViewClient(){

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub

				try{
					dismissDialog(0);
				}catch(IllegalArgumentException e){
					e.printStackTrace();
				}
				super.onPageFinished(view, url);
			}    
		});

		final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(   
				ViewGroup.LayoutParams.WRAP_CONTENT,   
				ViewGroup.LayoutParams.WRAP_CONTENT,   
				Gravity.BOTTOM + Gravity.CENTER_HORIZONTAL   
				);

		wv.setFocusable(false);
		wv.getSettings().setSupportZoom(true);
		wv.getSettings().setBuiltInZoomControls(true);

		/*FrameLayout mContentView = (FrameLayout)getWindow().getDecorView().findViewById(android.R.id.content);
		
		final View zoom = wv.getZoomControls();
		mContentView.removeView(zoom);
		mContentView.addView(zoom, ZOOM_PARAMS);
		zoom.setVisibility(View.GONE);*/
		
		
		int width = ((WindowManager) getSystemService(ActivityDeliveryDetail.WINDOW_SERVICE)).getDefaultDisplay().getWidth(); 
		double scale = (double)((double)width / (double)(480 + 10));
		wv.setInitialScale((int)(scale * 100));
		//wv.loadUrl(url);
		wv.getSettings().setDefaultTextEncodingName("UTF-8");
		showDialog(0);

		wv.loadUrl(mImageUrl);
		// TODO Auto-generated method stub
	}
	
	public void onClick(View v) {
		
		if(btnToggle.getText().equals("신촌")){
			pm.putItem("MODE_YIC", "");
			btnToggle.setText("국캠");
			wv.loadUrl(SINCHON_URL);
		}else if(btnToggle.getText().equals("국캠")){
			pm.putItem("MODE_YIC", "true");
			btnToggle.setText("신촌");
			wv.loadUrl(YIC_URL);
		}
		
	}
	
	@Override // 로딩바 안보이게 하기
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(this, R.style.Theme_Dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog_progress, null);
		dialog.setContentView(view);
		
		dialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				// TODO Auto-generated method stub
				finish();

			}

		});
		return dialog;

	}
	
	

}
